<?php
    //区域管理类
    class Area_manage extends CI_Model{

        public function __construct(){
            parent::__construct();
            $this->load->database();
        }

        //区域新增方法
        public function area_add($area_name, $area_location, $remarks, $area_createman){
            //如果区域名为空或地理位置为空则报错
            if ($area_name == '' || $area_location == '') {
                $return_fa = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return_fa);
            } else {
                //取区域编号最大的值
                $this->db->select_max('area_number');
                $query_area = $this->db->get('dorm_area');
                //区域编号值自己进行递增
                $area_number = $query_area->row()->area_number + 1;
                //取当前日期
                $date_now = date("Y-m-d");
                $data_area = array(
                  'area_name' => $area_name,          //区域名称
                  'area_number' => $area_number,      //区域编号
                  'area_location' => $area_location,  //区域地理位置
                  'area_createman' => $area_createman,//区域创建人
                  'area_cdate' => $date_now,          //区域创建时间
                  'remarks' => $remarks               //备注
                );
                //将数据插入区域表
                $this->db->insert('dorm_area', $data_area);
                $query = $this->db->affected_rows();
                if ($query) {  
                    //新增成功
                    $return_su = array('res' => '40000', 'msg' => '新增成功' ,'data' => $area_number);
                    $result = json_encode($return_su);
                } else {
                    //新增失败
                    $return_fa = array('res' => '40001', 'msg' => '新增失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }


        //区域删除方法
        public function area_delete($area_number){
            //取区域下的不同宿舍楼居住的人数的和
            $this->db->select_sum('liveNumber');
            $this->db->join('dorm_sslmanage', 'dorm_sslmanage.area_number = dorm_area.area_number', 'left');
            $this->db->where('dorm_area.area_number', $area_number);
            $query_area = $this->db->get('dorm_area');
            if ($query_area->num_rows() > 0) {
                //如果区域下的不同的宿舍楼居住的人数的和为零，允许删除此区域
                if ($query_area->row()->liveNumber == '') {
                    $this->db->delete('dorm_area', array('area_number' => $area_number));
                    $query_delete = $this->db->affected_rows();
                    if ($query_delete) {
                        //删除成功 
                        $return_su = array('res' => '40000', 'msg' => '删除成功' ,'data' => '');
                        $result = json_encode($return_su);
                    } else {
                        //删除失败
                        $return_fa = array('res' => '40001', 'msg' => '删除失败' ,'data' => '');
                        $result = json_encode($return_fa);
                    }
                } else {
                    //人数不为空不允许删除
                    $return_fa = array('res' => '40001', 'msg' => '入住人数不为空不允许删除', 'data' => '');
                    $result = json_encode($return_fa);
                }
            } else {
                //无此区域编号
                $return_fa = array('res' => '40001', 'msg' => '无此区域编号', 'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

      //区域修改方法
      public function area_update($area_name, $area_number, $area_location, $area_createman, $area_cdate, $remarks){
          //取区域修改的数组
          $area_data = array(
            'area_name' => $area_name,          //区域名称
            'area_number' => $area_number,      //区域编号
            'area_location' => $area_location,  //区域位置
            'area_createman' => $area_createman,//区域创建人
            'area_cdate' => $area_cdate,        //区域创建日期
            'remarks' => $remarks               //备注
          );
          //将收到的值更新表
          $this->db->where('area_number', $area_number);
          $this->db->update('dorm_area', $area_data);
          $query_area = $this->db->affected_rows();
          $data = $this->db->get_where('dorm_area', array('area_number' => $area_number));
          if ($query_area) {
              //修改成功
              $return_su = array('res' => '40000', 'msg' => '修改成功', 'data' => $data->row());
              $result = json_encode($return_su);
          } else {
              //修改失败     
              $result_fa = array('res' => '40001', 'msg' => '修改失败' ,'data' => '');
              $result = json_encode($result_fa);
          }
          return $result;
      }

      //区域查询方法-总条数
      public function area_select_all($area_number, $area_name){
          //拼接sql语句
          $sj1 = $area_number == '' ? ' 1 = 1 ' : " area_number LIKE '%" . $area_number . "%'";
          $sj2 = $area_name== '' ? ' AND 1 = 1 ' : " AND area_name LIKE '%" . $area_name . "%'";
          $sql = 'SELECT * FROM dorm_area WHERE ' . $sj1 . $sj2;
          $query = $this->db->query($sql, $sj1, $sj2);
          $all = $query->num_rows();
          return $all;
      }

      //区域查询方法
      public function area_select($area_number, $area_name, $limit ,$count)
      {
          //如果limit不为空才计算页数
          if($limit != ''){
              $page = ("$limit" - 1) * 10;
          }
          $sj1 = $area_number == '' ? ' 1 = 1 ' : " area_number LIKE '%" . $area_number . "%'";
          $sj2 = $area_name == '' ? ' AND 1 = 1 ' : " AND area_name LIKE '%" . $area_name . "%'";
          $sj3 = $limit == '' ? ' AND 1 = 1 ' : "limit " . $page . ",10";
          // $sql = 'SELECT * FROM dorm_area WHERE ' . $sj1 . $sj2 . " Limit " . ($limit - 1) * 10 . ",10";
          $sql = 'SELECT * FROM dorm_area WHERE ' . $sj1 . $sj2 . $sj3;
          //如果limit不为空按页数显示
          if($limit != ''){
              $query = $this->db->query($sql, $sj1, $sj2 ,$sj3);
          //如果页数为空则显示全部
          } else {
          $query = $this->db->query($sql, $sj1, $sj2);
          }
          // $sql1 = 'SELECT * FROM dorm_area';
          // $query1 = $this->db->query($sql1);
          if ($query->num_rows() > 0) {
              //查询成功
              $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result() ,'page' => $count);
              $result = json_encode($return_su);
          } else {
              //查询失败
              $return_fa = array('res' => '40001', 'msg' => '查询失败', 'data' => '');
              $result = json_encode($return_fa);
          }
          return $result;
      }

      //区域excel查询
      public function excel_area(){
          $query = $this->db->query("SELECT area_name ,area_number ,area_location ,area_createman ,area_cdate ,remarks FROM dorm_area");
          $result = $query->result();
          return json_encode(array('ret' => '40001', 'msg' => $result));
      }
}


